<template>
  <el-dialog :close-on-click-modal="closeAble"
             title="发布作业"
             :close-on-press-escape="closeAble"
             :visible.sync="dialogFormVisible"
             @close="close"
             @open="open">
    <el-form :model="form" ref="formRef">
      <el-form-item label="课程名称" :label-width="formLabelWidth">
        <el-input v-model="form.course" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="作业名称" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="作业内容" :label-width="formLabelWidth">
        <el-input type="textarea" rows="8" v-model="form.content" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
    export default {
        name: "WorkEditForm",
        props: {
            dialogFormVisible:{
                type:Boolean,
                default:false
            },
            type:{
                type:String,
                default:"add"
            },
            id:{
                type:Number,
                default:null
            }
        },
        data() {
            return {
                form: {
                    id: '',
                    course: '',
                    name: '',
                    content: '',
                },
                closeAble: false,
                formLabelWidth: '120px'
            }
        },
        mounted() {
        },
        methods: {
            close() {
                this.dialogFormVisible = false;
                this.$emit('onClose', null);
            },
            open() {
                if (this.type == "edit") {
                    this.getById();
                }
                this.form = {
                    id: '',
                    course: '',
                    name: '',
                    content: '',
                }
            },
            save() {
                this.form.publisher=this.$store.state.username;
                this.$axios.post("/workPublish/save", this.form).then(res => {
                    this.$message({
                        message: '保存成功!',
                        type: 'success'
                    });
                    this.close();
                }).catch(err => {
                    console.log("保存失败!");
                })
            },
            getById() {
                this.$axios.get("/workPublish/get/" + this.id).then(res => {
                    if (res.data && res.data.code == 200) {
                        this.form = res.data.result;
                    }
                    console.log(res);
                }).catch(err => {
                    console.log("保存失败!");
                })
            }
        }
    }
</script>

<style scoped>

</style>
